package com.nanyang.tms.service.company.impl;

import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.companytransaction.CompanyTransactionQueryRequest;
import com.nanyang.tms.api.dto.request.companytransaction.FrontTransactionQueryRequest;
import com.nanyang.tms.api.dto.response.companytransaction.CompanyTransactionResponse;
import com.nanyang.tms.api.dto.response.companytransaction.FrontTransactionResponse;
import com.nanyang.tms.api.service.companytransaction.CompanyTransactionRpcService;
import com.nanyang.tms.domain.company.CompanyTransactionRecordQuery;
import com.nanyang.tms.domain.company.CompanyTransactionRecordVO;
import com.nanyang.tms.manager.company.CompanyTransactionManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class CompanyTransactionRpcServiceImpl implements CompanyTransactionRpcService {
    @Autowired
    private CompanyTransactionManager companyTransactionManager;

    @Override
    public ApiResult<PageBean<CompanyTransactionResponse>> listTransaction(CompanyTransactionQueryRequest param) {
        ApiResult<PageBean<CompanyTransactionResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyTransactionRecordQuery query = new CompanyTransactionRecordQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<CompanyTransactionRecordVO> companyStoredList = companyTransactionManager.listTransaction(query);
            PageBean<CompanyTransactionResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(companyStoredList.getTotalItem());
            BeanUtils.copyListProperties(companyStoredList.getData(), resultList.getData(), CompanyTransactionResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }
    @Override
    public ApiResult<PageBean<FrontTransactionResponse>> listFrontTransaction(FrontTransactionQueryRequest param) {
        ApiResult<PageBean<FrontTransactionResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyTransactionRecordQuery query = new CompanyTransactionRecordQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<CompanyTransactionRecordVO> listTransaction = companyTransactionManager.listTransaction(query);
            PageBean<FrontTransactionResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(listTransaction.getTotalItem());
            BeanUtils.copyListProperties(listTransaction.getData(), resultList.getData(), FrontTransactionResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }
}
